<?php

namespace app\admin\controller;

use app\admin\model\Vipuserlist;
use app\admin\model\ZoneList;
use app\common\controller\Backend;
use think\exception\DbException;
use think\Model;
use think\response\Json;

class Vipuser extends Backend
{
    /**
     * Anchor模型对象
     * @var \app\admin\model\Anchor
     */
    protected $model = null;
    protected $zone = null;
    protected $vipuser_list = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Vipuser();

        $this->vipuser_list = new Vipuserlist();

        $this->zone = new ZoneList();

        $zoneList = $this->zone->all();
        $zoneData = [];
        foreach ($zoneList as $z) {
            $zoneData[$z->zone] = $z->zonename;
        }

        $this->assignconfig("zoneList", $zoneData);
    }

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();

        $list = $this->model
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);

        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function vipfind()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            $this->assignconfig("grade", [
                ['id' => "-1", "name" => '未知'],
                ['id' => "0", "name" => '放弃'],
                ['id' => "1", "name" => '一般'],
                ['id' => "2", "name" => '优质'],
            ]);

            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();

        $where = json_decode($this->request->get("filter", ''), true);
        if (isset($where['zone_list.zonename']) && !empty($where['zone_list.zonename'])) {
            $where['zone_list.zone'] = $where['zone_list.zonename'];
            unset($where['zone_list.zonename']);
        }

        $list = $this->vipuser_list
            ->where($where)
            ->with('zoneList')
            ->order($sort, $order)
            ->paginate($limit);

        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }
}